#include "Obj.h"
#include "shape.h"

using namespace std;
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/gl.h>										
#include <GL/glu.h>
#include <vector>
#include <iostream>
#include <string>
#include <fstream>

std::vector<Vector> Obj::points;
std::vector<std::vector<int>> Obj::faces;

Obj::Obj(){}

void Obj::LoadObj(std::string chemin){


	
	std::ifstream objFile(chemin.c_str(), std::ios::in);
	
	if(!objFile)
	{
		std::cout << "Error : openning obj file (" << path << ")" << std::endl;
		exit(1);
	}
	
	std::vector<Geometry::Point*> points;
	std::string line;
	
	while(std::getline(objFile, line))
	{
		if(line[0] == 'v' && line[1] == ' ') points.push_back(new Geometry::Point(line.c_str()));
		if(line[0] == 'f' && line[1] == ' ')
		{
			Geometry::Face* face = new Geometry::Face();
			int pt1, pt2, pt3, pt4;
			if(typeFace(line) == 3)
			{
				sscanf(line.c_str() + 2, "%d %d %d", &pt1, &pt2, &pt3);
				face->p.push_back(points[pt1-1]);
				face->p.push_back(points[pt2-1]);
				face->p.push_back(points[pt3-1]);
				
			}
			if(typeFace(line) == 4)
			{
				sscanf(line.c_str() + 2, "%d %d %d %d", &pt1, &pt2, &pt3, &pt4);
				face->p.push_back(points[pt1-1]);
				face->p.push_back(points[pt2-1]);
				face->p.push_back(points[pt3-1]);
				face->p.push_back(points[pt4-1]);
			}
			mesh->f.push_back(face);
		}
	}
	objFile.close();
	
	return mesh;
}